package com.example.ecommercedemo.controller;

import com.example.ecommercedemo.entity.Banners;
import com.example.ecommercedemo.utils.PageResult;
import com.example.ecommercedemo.utils.PassToken;
import com.example.ecommercedemo.utils.ServerResponse;
//import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.example.ecommercedemo.service.IBannersService;


@RestController
@RequestMapping("/system/banners")
public class BannersController
{
    @Autowired
    private IBannersService bannersService;

//    /**
//     * 查询banners列表
//     */
    @PassToken
    @GetMapping("/list")
    public ServerResponse list(Banners banners, @RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "4")  int pageSize)
    {
//        startPage();
        //分页
//        List<Banners> list = bannersService.selectBannersList(banners);
//
//        return ServerResponse.createBySuccess(200,,"查询成功");
        PageResult pageResult = bannersService.selectBannersList(banners,pageNum,pageSize);

        return ServerResponse.createBySuccess(200,pageResult,"获取成功");
    }

//    /**
//     * 导出banners列表
//     */
//    @PreAuthorize("@ss.hasPermi('system:banners:export')")
//    @Log(title = "banners", businessType = BusinessType.EXPORT)
//    @PostMapping("/export")
//    public void export(HttpServletResponse response, Banners banners)
//    {
//        List<Banners> list = bannersService.selectBannersList(banners);
//        ExcelUtil<Banners> util = new ExcelUtil<Banners>(Banners.class);
//        util.exportExcel(response, list, "banners数据");
//    }

//    /**
//     * 获取banners详细信息
//     */
    @GetMapping(value = "/{id}")
    public ServerResponse getInfo(@PathVariable("id") Long id)
    {   Banners banners=bannersService.selectBannersById(id);

            return ServerResponse.createBySuccess(200,banners,"查询成功");

    }

//    /**
//     * 新增banners
//     */

    @PostMapping
    public ServerResponse add(@RequestBody Banners banners)
    {   int i=bannersService.insertBanners(banners);
        if(i>0){
            return  ServerResponse.createBySuccess(200,"","添加成功");
        }else{
            return ServerResponse.createByFail(500,"添加失败");
        }

    }

//    /**
//     * 修改banners
//     */
    @PutMapping
    public ServerResponse edit(@RequestBody Banners banners)
    {   int i=bannersService.updateBanners(banners);
        if(i>0){
            return  ServerResponse.createBySuccess(200,"","更新成功");
        }else{
            return ServerResponse.createByFail(500,"更新失败");
        }

    }

//    /**T
//     * 删除banners
//     */
	@DeleteMapping("/{ids}")
    public ServerResponse remove(@PathVariable Long[] ids)
    {   int i=bannersService.deleteBannersByIds(ids);
        if(i>0){
            return  ServerResponse.createBySuccess(200,"","删除成功");
        }else{
            return ServerResponse.createByFail(500,"删除失败");
        }

    }
}
